package com.fling.screen;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator.FreeTypeFontParameter;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.fling.Constants;
import com.fling.FlingHero;
import com.fling.Locator;
import com.fling.observer.Event;
import com.fling.observer.Subject;

public class PauseScreen implements Constants {
	private BitmapFont white;
	private Stage stage;
	private Table table;
	private TextButton buttonResume, buttonMenu;
	private Label heading;
	private Skin skin;
	private TextureAtlas atlas;
	private TextButtonStyle textButtonStyle;
	private ShapeRenderer sh;
	private Subject subject;
	private boolean added;
	private final ArrayList<TransitionEffect> effects;

	public PauseScreen(final FlingHero game, Screen parent, final Subject sub) {
		sh = new ShapeRenderer();
		stage = new Stage();
		added = false;
		
		FileHandle fontFile = Gdx.files.internal(FONT);
	    FreeTypeFontGenerator generator = new FreeTypeFontGenerator(fontFile);
	    FreeTypeFontParameter param=new FreeTypeFontParameter();
	    param.size=(int) (96/UI_SCALE);
	    white = generator.generateFont(param);
	    generator.dispose();
	    
		atlas = new TextureAtlas(Gdx.files.internal("data/button.pack"));
		skin = new Skin(atlas);
		table = new Table(skin);
		this.subject = sub;
		effects = new ArrayList<TransitionEffect>();

		textButtonStyle = new TextButtonStyle();
		textButtonStyle.up = skin.getDrawable("button.up");
		textButtonStyle.down = skin.getDrawable("button.down");
		textButtonStyle.pressedOffsetX = 1;
		textButtonStyle.pressedOffsetY = -1;
		textButtonStyle.font = white;

		final Screen current = parent;
		// create buttons
		buttonResume = new TextButton("Resume", textButtonStyle);
		buttonResume.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(null, Event.EVENT_CLICK);
				subject.notify(null, Event.EVENT_UNPAUSE);
			}
		});
		buttonResume.pad(10.0f);

		buttonMenu = new TextButton("Menu", textButtonStyle);
		buttonMenu.addListener(new ClickListener() {
			@Override
			public void clicked(InputEvent event, float x, float y) {
				subject.notify(null, Event.EVENT_CLICK);
				game.getGameScreen().restart();
				Screen transitionScreen = new TransitionScreen(game, current, game.getMenuScreen(), effects);
				Locator.getGame().setScreen(transitionScreen);
			}
		});
		buttonMenu.pad(10.0f);

		// create title
		LabelStyle headingStyle = new LabelStyle(white, Color.WHITE);
		heading = new Label("Paused", headingStyle);
		heading.setFontScale(1.0f);
		table.add(heading).expandY().center();
		table.row();
		table.add(buttonResume).center();
		table.row();
		table.add(buttonMenu).expandY().center();

		stage.addActor(table);

	}

	public void render(float delta) {
		Gdx.gl.glEnable(GL20.GL_BLEND);
		Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
		sh.begin(ShapeType.Filled);
		sh.setColor(0, 0, 0, 0.75f);
		sh.rect(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		sh.end();
		Gdx.gl.glDisable(GL20.GL_BLEND);
		stage.act(delta);
		stage.draw();
	}

	public void pause() {
		if (!added) {
			Locator.getInputMultiplexer().addProcessor(stage);
		}
		effects.clear();
		effects.add(new FadeOutTransitionEffect(0.5f));
		effects.add(new FadeInTransitionEffect(0.5f));
		added = true;
	}

	public void resume() {
		Locator.getInputMultiplexer().removeProcessor(stage);
		added=false;

	}

	public void resize(int width, int height) {
		stage.getViewport().update(width, height, true);
		table.invalidateHierarchy();
		table.setSize(width, height);

	}

}
